﻿/***************************
* 1.游记列表
* 2.游记详情
****************************/

/* 变量 */
var travelnote = "experience/travelnote.html";
var travelnoteDetail = "experience/travelnoteDetail.html";

/* 游记 */
var contactsCallback_travelnote = myApp.onPageInit('travelnote', function (page) {

    var index_tab1 = 1;
    var index_tab2 = 1;
    var index_tab3 = 1;
    var current_tabindex = 0;
    var size = 10;

    LoadData(current_tabindex, index_tab1, size);

    //加载数据
    function LoadData(tabindex, pageindex, pagesize) {
        var conditionParams;

        if (tabindex == 0) {
            conditionParams = {
                type: 2,
                content: JSON.stringify({
                    calltype: "0",
                    jsonParm: {
                        pageIndex: pageindex,
                        pageSize: pagesize,
                        sortBy: "1",
                    },
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 1) {
            conditionParams = {
                type: 2,
                content: JSON.stringify({
                    calltype: "0",
                    jsonParm: {
                        pageIndex: pageindex,
                        pageSize: pagesize,
                        sortBy: "3",
                    },
                    isApp: "1",
                }),
            };
        }
        else if (tabindex == 2) {
            conditionParams = {
                type: 2,
                content: JSON.stringify({
                    calltype: "0",
                    jsonParm: {
                        pageIndex: pageindex,
                        pageSize: pagesize,
                        sortBy: "2",
                    },
                    isApp: "1",
                }),
            };
        }

        myApp.showPreloader();
        $$.ajax({
            url: ISS_Constants.Url.const_sns_url,
            data: conditionParams,
            dataType: "jsonp",
            success: function (datas, status) {
                myApp.hidePreloader();
                loading=false;
                data = eval('(' + datas + ')');
                if (data && data.result && data.result.length > 0) {
                    // 格式化数据
                    for (var i = 0; i < data.result.length; i++) {
                        data.result[i].type = current_tabindex;
                        // 默认图片
                        if (data.result[i].mp_cover == undefined || data.result[i].mp_cover == null || data.result[i].mp_cover == "") {
                            data.result[i].mp_cover = NoPicture();
                        }
                    }

                    if (current_tabindex == 0) {
                        var searchTemplate = $$('#template-travelnote-tab1').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_tab1 .content-block ul").append(html);
                            index_tab1++;
                        }
                    }
                    else if (current_tabindex == 1) {
                        var searchTemplate = $$('#template-travelnote-tab1').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_tab2 .content-block ul").append(html);
                            index_tab2++;
                        }
                    }
                    else if (current_tabindex == 2) {
                        var searchTemplate = $$('#template-travelnote-tab1').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_tab3 .content-block ul").append(html);
                            index_tab3++;
                        }
                    }

                    if (current_tabindex == 0 && $$("#sub_tab1 .content-block ul li").length >= data.TotalRecords) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 1 && $$("#sub_tab2 .content-block ul li").length >= data.TotalRecords) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader2').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 2 && $$("#sub_tab3 .content-block ul li").length >= data.TotalRecords) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader3').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    // 清除提示
                    $$(".noAnswer").remove();
                    // 添加提示
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        $$("#sub_tab1 .content-block").html(ISS_Element.DIV.noAnswer);
                        $$("#sub_tab1 .noAnswer").on("click", function () {
                            index_tab1 = 1;
                            LoadData(index_tab1, current_tabindex);
                        });
                    }
                    else if (current_tabindex == 1) {
                        $$('.infinite-scroll-preloader2').remove();
                        $$("#sub_tab2 .content-block").html(ISS_Element.DIV.noAnswer);
                        $$("#sub_tab2 .noAnswer").on("click", function () {
                            index_tab2 = 1;
                            LoadData(index_tab2, current_tabindex);
                        });
                    }
                    else if (current_tabindex == 2) {
                        $$('.infinite-scroll-preloader3').remove();
                        $$("#sub_tab3 .content-block").html(ISS_Element.DIV.noAnswer);
                        $$("#sub_tab3 .noAnswer").on("click", function () {
                            index_tab3 = 1;
                            LoadData(index_tab3, current_tabindex);
                        });
                    }
                }
            },
            error: function (xhr, status) {
                loading=false;
                myApp.hidePreloader();
                myApp.alert("服务器连接失败！");
             }
        });
    }; // end load

    //游记切换
    $$(".subnavbar").on("click", "a", function () {
        $$('.infinite-scroll').scrollTop(0);
        current_tabindex = $$(this).index();

        if (current_tabindex == 0 && $$('.infinite-scroll-preloader1').length > 0) {
            myApp.attachInfiniteScroll($$('.infinite-scroll'));
            if ($$("#sub_tab1 ul").children("li").length == 0) {
                LoadData(current_tabindex, index_tab1, size);
            }
        }
        else if (current_tabindex == 1 && $$('.infinite-scroll-preloader2').length > 0) {
            myApp.attachInfiniteScroll($$('.infinite-scroll'));
            if ($$("#sub_tab2 ul").children("li").length == 0) {
                LoadData(current_tabindex, index_tab2, size);
            }
        }
        else if (current_tabindex == 2 && $$('.infinite-scroll-preloader3').length > 0) {
            myApp.attachInfiniteScroll($$('.infinite-scroll'));
            if ($$("#sub_tab3 ul").children("li").length == 0) {
                LoadData(current_tabindex, index_tab3, size);
            }
        }
    }); // end tab

    // 加载flag
    var loading = false;
    // 注册无限加载'infinite'事件处理函数
    $$('.infinite-scroll').on('infinite', function () {

        // 如果正在加载，则退出
        if (loading) return;

        // 设置flag
        loading = true;

        // 模拟1s的加载过程
        setTimeout(function () {
            

            if (current_tabindex == 0) {
                LoadData(current_tabindex, index_tab1, size);
            }
            else if (current_tabindex == 1) {
                LoadData(current_tabindex, index_tab2, size);
            }
            else if (current_tabindex == 2) {
                LoadData(current_tabindex, index_tab3, size);
            }
        }, 1000);
    }); // end infinite
});

/* 游记详情  */
var contactsCallback_travelnoteDetail = myApp.onPageInit('travelnoteDetail', function (page) {
    //获取页面参数
    var code = ISS.utils.getParameter("code");
    var dataCategoryParm = ISS.utils.getParameter("dataCategoryParm");
    if (page != undefined) {
        code = page.query.code;
        dataCategoryParm = page.query.dataCategoryParm;
    }

    // var conditionParams = {
    //     type: 2,
    //     content: JSON.stringify({
    //         t: dataCategoryParm,
    //         kw: code
    //     }),
    // };


    var conditionParams = {
        calltype: "1",
        callback: "a",
        jsonParm: JSON.stringify({
            threadID: code
        })
    }

    $$.ajax({
        // url: ISS_Constants.Url.const_sns_url,
        url: ISS_Constants.Url.const_forum_url,
        data: conditionParams,
        crossDomain: true,
        method: "GET",
        dataType: "json",
        success: function (data, status) {
            // data = data.toString();
            // data = data.substring(2, data.length - 1);
            // data = JSON.parse(data);
            if (data && data.result && data.result.length > 0) {
                // 默认图片
                if (data.result[0].mh_cover == undefined || data.result[0].mh_cover == null || data.result[0].mh_cover == "") {
                    data.result[0].mh_cover = NoPicture();
                }
                data.result[0].mp_content = decodeURIComponent(data.result[0].mp_content);
                var searchTemplate = $$('#template-travelnote-detail').html();
                if (searchTemplate) {
                    var compiledTemplate = Template7.compile(searchTemplate);
                    var html = compiledTemplate(data.result[0]);
                    $$("#travelnote_detail_page .page-content").html(html);
                }
            }
        },
        error: function (xhr, status) { }
    });
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(travelnote) > -1) {
        contactsCallback_travelnote.trigger();
    }
    else if (href.indexOf(travelnoteDetail) > -1) {
        contactsCallback_travelnoteDetail.trigger();
    }
}

initData();


